package com.ukao.steward.adapter;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ukao.steward.R;
import com.ukao.steward.base.ListBaseAdapter;
import com.ukao.steward.base.SuperViewHolder;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.bean.WaitingOrderBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.util.ActivityUtils;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.DecimalUtil;
import com.ukao.steward.util.MyDateUtils;
import com.ukao.steward.widget.StateButton;

import java.util.Date;
import java.util.List;

import static com.ukao.steward.R.drawable.shape_strok_green;
import static com.ukao.steward.R.drawable.shape_strok_red;
import static com.ukao.steward.R.id.createTime;
import static com.ukao.steward.R.id.totalAmount;

/**
 * Created by ukaodev on 2017/8/24.
 */

public class WaitingOrderAdapter extends ListBaseAdapter<WaitingOrderBean.ListBean> {




    public WaitingOrderAdapter(Context context, List<WaitingOrderBean.ListBean> mHomeBean) {
        super(context, mHomeBean);
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_waiting_order;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder,final int position) {

        LinearLayout takePhoneLayout=holder.getView(R.id.takePhone_layout);
        TextView business=holder.getView(R.id.business);
        TextView orderNo=holder.getView(R.id.orderNo);
        TextView takeDate=holder.getView(R.id.takeDate);
        TextView takeName=holder.getView(R.id.takeName);
        TextView takePhone=holder.getView(R.id.takePhone);
        TextView takeAddress=holder.getView(R.id.takeAddress);
        TextView userRemark=holder.getView(R.id.userRemark);
        TextView createTime=holder.getView(R.id.createTime);

        StateButton robOrdidBtn=holder.getView(R.id.robOrdid_btn);
        StateButton refuseBtn=holder.getView(R.id.refuse_btn);
        TextView payStatus=holder.getView(R.id.payStatus);
        TextView clothingCnt=holder.getView(R.id.clothing_cnt);
        TextView totalAmountxt=holder.getView(totalAmount);

        WaitingOrderBean.ListBean item = mDataList.get(position);
        String  takeTime=item.getTakeDate();
        String  takeTimeStart=item.getTakeTimeStart();
        String     takeTimeEnd=item.getTakeTimeEnd();
        orderNo.setText(item.getOrderNo());
        takeDate.setText(!CheckUtils.isEmpty(takeTime) && !CheckUtils.isEmpty(takeTimeStart) ? takeTime + " " + takeTimeStart + "-" + takeTimeEnd : "");
        takePhone.setText(item.getTakePhone());
        takeName.setText(item.getTakeName());
        takeAddress.setText(item.getTakeAddress());
        userRemark.setText(item.getUserRemark());
        createTime.setText(MyDateUtils.getTimeFormatText(item.getCreateTime()));
        payStatus.setText(item.getPayStatusText());
        if (item.getPayStatus()){
            totalAmountxt.setTextColor(getColors(R.color.greend_color) );
            payStatus.setBackgroundResource(R.drawable.shape_strok_green);
            payStatus.setTextColor(getColors(R.color.greend_color) );
        }else{
            payStatus.setBackgroundResource(R.drawable.shape_strok_red);
            payStatus.setTextColor(getColors(R.color.red));
            totalAmountxt.setTextColor(getColors(R.color.red) );
        }


        if (!CheckUtils.isEmpty(item.getBusiness())){
            business.setText(Constant.getbusiness(item.getBusiness()));
        }else{
            business.setText("");
        }


            String  proPriceDesc=item.getProPriceDesc();

            if (!CheckUtils.isEmpty(proPriceDesc)){

                Gson    mGson=new Gson();
                List<StringBean>   mproPriceDesc= mGson.fromJson(proPriceDesc,
                        new TypeToken<List<StringBean>>() {
                        }.getType());
               int totalCnt=0;
                String  totalAmount="";
                for (StringBean stringBean : mproPriceDesc) {
                    String unitPrice = CheckUtils.isEmptyNumber(stringBean.getPrice());
                    String  discounts=CheckUtils.isDiscount(stringBean.getDiscount());
                    totalCnt=totalCnt+stringBean.getCnt();
                    if (CheckUtils.isEmpty(discounts)){
                        String    subtotal=DecimalUtil.multiply(stringBean.getCnts(),unitPrice);
                        totalAmount=DecimalUtil.add(totalAmount,subtotal);
                    }else{
                        String subtotal = DecimalUtil.multiply(stringBean.getCnts(), DecimalUtil.multiply(unitPrice, CheckUtils.isEmptyNumber(stringBean.getDiscount())));
                        totalAmount=DecimalUtil.add(totalAmount,subtotal);
                    }

                }
                clothingCnt.setText(totalCnt+"");
                totalAmountxt.setText("￥"+totalAmount);
            }else{
                totalAmountxt.setText("");
            }


        View.OnClickListener  OnClickListener= v -> {
            WaitingOrderBean.ListBean item1 = mDataList.get(position);
            mOnItemChildClickListener.onChildItemClick(v.getId(), position, item1);
            if (v.getId()==R.id.takePhone_layout){
                if (!CheckUtils.isEmpty(item1.getTakePhone())){
                    ActivityUtils.callActivity(mContext, item1.getTakePhone());
                }

            }
        };

        takePhoneLayout.setOnClickListener(OnClickListener);
        robOrdidBtn.setOnClickListener(OnClickListener);
        refuseBtn.setOnClickListener(OnClickListener);
    }



}
